﻿/***********************************************************************
 * <copyright file="NewsLogRepository.cs" company="BUCA JSC">
 * -->    Copyright (C) statement. All right reserved
 * </copyright>
 * 
 * Created:  Nguyen Chi Binh
 * Email:    nguyentribinh@live.com
 * Website:
 * Create Date: Friday, April 11, 2014
 * Usage: 
 * 
 * RevisionHistory: 
 * Date         Author               Description 
 * 
 * ************************************************************************/
using System;
using System.Collections.Generic;
using System.Linq;
using DXWebApplication.NewsServiceReference;
using DXWebApplication.Repositories.Core;
using DXWebApplication.Repositories.Core.News;
using NmResources;

namespace DXWebApplication.Repositories.Implementations.News
{
    /// <summary>
    /// NewsLogRepository class
    /// </summary>
    public class NewsLogRepository : NewsRepositoryBase, INewsLogRepository
    {
        /// <summary>
        /// Gets the list.
        /// </summary>
        /// <param name="criterion">The criterion.</param>
        /// <returns></returns>
        public List<NewsLogDto> GetList(Criterion criterion = null)
        {
            var request = new NewsLogRequest().Prepare();
            request.LoadOptions = new[] { "NewsLogs" };
            var response = NewsServiceClient.GetNewsLogs(request);
            Correlate(request, response);
            return response.Items.ToList();
        }

        /// <summary>
        /// Gets the paged.
        /// </summary>
        /// <param name="totalPage">The total page.</param>
        /// <param name="currentPage">The current page.</param>
        /// <returns></returns>
        public List<NewsLogDto> GetPaged(ref int totalPage, int currentPage = 1)
        {
            var request = new NewsLogRequest().Prepare();
            request.LoadOptions = new[] { "NewsLogs" };
            request.OptionTypes = OptionType.IsPaging;
            request.CurrentPage = currentPage;
            request.TotalPage = totalPage;
            var response = NewsServiceClient.GetNewsLogs(request);
            Correlate(request, response);
            return response.Items.ToList();
        }

        /// <summary>
        /// Gets the specified identifier.
        /// </summary>
        /// <param name="id">The identifier.</param>
        /// <returns></returns>
        public NewsLogDto Get(int id)
        {
            var request = new NewsLogRequest().Prepare();
            request.LoadOptions = new[] { "NewsLog" };
            request.Criteria = new NewsLogCriteria {Id = id};
            var response = NewsServiceClient.GetNewsLogs(request);
            Correlate(request, response);
            return response.Item;
        }

        /// <summary>
        /// Inserts the specified t.
        /// </summary>
        /// <param name="t">The t.</param>
        /// <returns></returns>
        /// <exception cref="System.ApplicationException"></exception>
        public string Insert(NewsLogDto t)
        {
            var request = new NewsLogRequest().Prepare();
            request.Action = "Insert";
            request.Item = t;
            var response = NewsServiceClient.SetNewsLogs(request);
            Correlate(request, response);
            // These messages are for public consumption. Includes validation errors.
            if (response.Acknowledge == AcknowledgeType.Failure)
                throw new ApplicationException(response.Message);
            return response.MessageCode.MapperResource();
        }

        /// <summary>
        /// Updates the specified t.
        /// </summary>
        /// <param name="t">The t.</param>
        /// <returns></returns>
        /// <exception cref="System.ApplicationException"></exception>
        public string Update(NewsLogDto t)
        {
            var request = new NewsLogRequest().Prepare();
            request.Action = "Update";
            request.Item = t;
            var response = NewsServiceClient.SetNewsLogs(request);
            Correlate(request, response);
            // These messages are for public consumption. Includes validation errors.
            if (response.Acknowledge == AcknowledgeType.Failure)
                throw new ApplicationException(response.Message);
            return response.MessageCode.MapperResource();
        }

        /// <summary>
        /// Deletes the specified identifier list.
        /// </summary>
        /// <param name="idList">The identifier list.</param>
        /// <returns></returns>
        /// <exception cref="System.ApplicationException"></exception>
        public string Delete(IList<int> idList)
        {
            var request = new NewsLogRequest().Prepare();
            request.Action = "Delete";
            request.Criteria = new NewsLogCriteria {IdList = string.Join(",", idList)};
            var response = NewsServiceClient.SetNewsLogs(request);
            Correlate(request, response);
            // These messages are for public consumption. Includes validation errors.
            if (response.Acknowledge == AcknowledgeType.Failure)
                throw new ApplicationException(response.Message);
            return response.MessageCode.MapperResource();
        }

        /// <summary>
        /// Correlates requestid with returned response correlationId.
        /// These must always match. If not, request and responses are not related.
        /// </summary>
        /// <param name="request"></param>
        /// <param name="response"></param>
        /// <exception cref="System.ApplicationException">RequestId and CorrelationId do not match.</exception>
        protected new void Correlate(RequestBase request, ResponseBase response)
        {
            if (request.RequestId != response.CorrelationId)
                throw new ApplicationException("RequestId and CorrelationId do not match.");
        }
    }
}